#!/usr/bin/env python 2.7
# -*- coding: utf-8 -*-
# @Time      :2019/4/10
# @Author    :breeze
# @Email     :wangbaoshan@bbdservice.com
# @File      :IC25_OverdueSummaryIsNa
# @Software  :PyCharm


'''eof
name:逾期信息汇总表是否缺失
code:IC25_OverdueSummaryIsNa_2D
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import json
import sys
import re
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def intCheck(str):
    try:
        int(str)
        return True
    except:
        return False


def getPC02DH(data):
    # 信贷交易信息概要PCO[1.1]
    PCO = getTable(data, 'PCO', 1,0)
    if PCO == [{}]:
        return u'缺失值'
    elif PCO == 0:
        return True
    else:
        # 信贷交易信息概要信息单元PC02[0.1]
        PC02 = getTable(PCO[0], 'PC02', 0,0)
        if PC02 == [{}]:
            return u'缺失值'
        elif PC02 == 0:
            return True
        else:
            # 逾期（透支）汇总信息段PC02D[0,1]
            PC02D = getTable(PC02[0], 'PC02D', 0,1)
            if PC02D == [{}]:
                return u'缺失值'
            elif PC02D == 0:
                return True
            else:
                # 逾期（透支）汇总信息PC02DH[1.6]
                PC02DH = getTable(PC02D[0], 'PC02DH', 1,1)
                if PC02DH == [{}]:
                    return u'缺失值'
                elif PC02DH == 0:
                    return True
                else:
                    for i in PC02DH:
                        # 账户数
                        PC02DS02 = i.get("PC02DS02")
                        if PC02DS02 not in null_type_list and PC02DS02 != 0 and PC02DS02 != '0':
                            return False
                    return True


def getPD01EH(data):
    # 借贷账户信息PDA[1.1]
    PDA = getTable(data, 'PDA', 1,0)
    if PDA == [{}]:
        return u'缺失值'
    elif PDA == 0:
        return True
    else:
        # 借贷账户信息单元PD01[0.*]
        PD01 = getTable(PDA[0], 'PD01', 0,1)
        if PD01 == [{}]:
            return u'缺失值'
        elif PD01 == 0:
            return True
        else:
            for i in PD01:
                # 最近5年内的历史表现信息段PD01E[0.1]
                PD01E = getTable(i, 'PD01E', 0,1)
                if PD01E == [{}] or PD01E == 0:
                    continue
                else:
                    # 历史表现信息PD01EH[1.60]
                    PD01EH = getTable(PD01E[0], 'PD01EH', 1,1)
                    if PD01EH == [{}] or PD01EH == 0:
                        continue
                    else:
                        for j in PD01EH:
                            # 还款状态
                            PD01ED01 = j.get("PD01ED01")
                            if PD01ED01 not in null_type_list and intCheck(PD01ED01) and 1 <= int(PD01ED01) <= 7:
                                return False
                        return True


def overdueSummaryIsNa():
    data = IC_DSJ_2D['data']
    factor1 = getPC02DH(data)
    factor2 = getPD01EH(data)

    if factor1 == u'缺失值' or factor2 == u'缺失值':
        return u'缺失值'
    elif factor1 and factor2:
        return 1
    else:
        return 0


if __name__ == '__main__':
    file_obj = open(r'../../data/IC/IC25_OverdueSummaryIsNa/IC.json', 'r')
    content = file_obj.read()
    IC_DSJ_2D = json.loads(content, strict=False)
    aa = overdueSummaryIsNa()
    print aa

result = overdueSummaryIsNa()
